<template>
    <img ref="authimg" src="" alt="">
</template>
<script>
export default {
  name: "TopTools",
  props: {
    authsrc: {
      type: String,
      default: ''
    },
    imgid: {
      type: String | Number,
      default: ''
    }
  },
  components: {

  },
  data () {
    return {
    };
  },
  methods: {
    getBase64Image (img) {
      var canvas = document.createElement("canvas");
      canvas.width = img.width;
      canvas.height = img.height;
      var ctx = canvas.getContext("2d");
      ctx.drawImage(img, 0, 0, img.width, img.height);
      var dataURL = canvas.toDataURL("image/png");
      return dataURL;
      // return dataURL.replace("data:image/png;base64,", ""); 
    },
    loadImg (url) {
      let img = this.$refs.authimg;
      let request = new XMLHttpRequest();
      request.responseType = 'blob';
      request.open('get', url, true);

      request.setRequestHeader('requestType', 'app');
      request.setRequestHeader('applyID', '95b71316cebe4a18906066d9799faa59');
      request.setRequestHeader('secretKey', '13FFDB8B49D6568D78EB907AFBB6C17D');
      request.setRequestHeader('accessToken', this.$db.get('accessToken'));

      request.onreadystatechange = e => {
        if (request.readyState == XMLHttpRequest.DONE && request.status == 200) {
          img.src = URL.createObjectURL(request.response);
          img.onload = () => {
            URL.revokeObjectURL(img.src);
            this.$emit('get', this.getBase64Image(img));
          };
        }
      };
      request.send(null);
    }
  },
  mounted: function () {
    var vm = this;
    let url = vm.authsrc;
    if (!url) {
      vm.$getAndLoad('图片', 'searchfileByIdJK.do?fileid=' + vm.imgid, function (data) {
        if (data.code == '1') {
          var filesrc = 'http://183.207.196.25:50087/ga/jygawebcitycrisis/JYGAWebCityCrisis/downloadfileJK.do?fileallname=' + data.data.fileallname;
          vm.loadImg(filesrc);
        }
      });
    } else {
      vm.loadImg(vm.authsrc);
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
